package chances.webui.taglibs;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;

import chances.webui.model.Input;

@SuppressWarnings("serial")
public class InputTag extends WebUITag {
	protected Input input;
    private boolean split;
    private boolean remove = false;
    private String onRemove;
    

    public String getOnRemove() {
        return onRemove;
    }

    public void setOnRemove(String onRemove) {
        this.onRemove = onRemove;
    }

    @Override
	public int doStartTag() throws JspException {
		FormTag formTag = (FormTag) findAncestorWithClass(this, FormTag.class);
		if (formTag != null) {
			formTag.addInput(input);
		}
		return super.doStartTag();
	}

	@Override
	protected void initContext(Map<String, Object> context) {
		context.put("input", input);
		FormTag formTag = (FormTag) findAncestorWithClass(this, FormTag.class);
		if (formTag != null) {
			context.put("form", formTag);
		}
	}

	public void setInputClass(String inputClass) {
		input.setInputClass(inputClass);
	}

	public void setLabelClass(String labelClass) {
		input.setLabelClass(labelClass);
	}

	@SuppressWarnings("unchecked")
	public void setLookup(Object lookup) {
		if (lookup instanceof String) {
			try {
				LinkedHashMap<String, String> map 
					= objectMapper.readValue(((String)lookup).replace("'", "\""), LinkedHashMap.class);
				input.setLookup(map);
			} catch (Exception e) {
			}
		}else {
			input.setLookup(lookup);
		}
	}

	public void setStyle(String style) {
		input.setStyle(style);
	}

	public void setType(String type) {
		input.setType(type);
	}

	public void setLabelWidth(String labelWidth) {
		input.setLabelWidth(labelWidth);
	}

	public void setInputWidth(String inputWidth) {
		input.setInputWidth(inputWidth);
	}

	public void setPlaceholder(String placeholder) {
		input.setPlaceholder(placeholder);
	}

	public void setValue(Object value) {
		input.setValue(value);
	}
	
	

	public Object getValue() {
		return input.getValue();
	}

	public void setLabel(String label) {
		input.setLabel(label);
	}

	public void setTips(String tips) {
		input.setTips(tips);
	}

	public void setMultiple(boolean multiple) {
		input.setMultiple(multiple);
	}

	public void setDisableOnUpdate(boolean disableOnUpdate) {
		input.setDisableOnUpdate(disableOnUpdate);
	}

	public void setKey(boolean key) {
		input.setKey(key);
	}

	public void setHiddenOnUpdate(boolean hiddenOnUpdate) {
		input.setHiddenOnUpdate(hiddenOnUpdate);
	}

	public void setRequired(boolean required) {
		input.setRequired(required);
	}

	@Override
	protected void reset() {
		super.reset();
		this.input = new Input();
	}

	public void setChecked(boolean checked) {
		input.setChecked(checked);
	}

	public void setId(String id) {
		input.setId(id);
		super.setId(id);
	}

	public void setField(String field) {
		input.setField(field);
	}

	@Override
	public void setName(String name) {
		super.setName(name);
		input.setName(name);
		input.setField(name);
	}

	public Input getInput() {
		return input;
	}

	public boolean isSplit() {
		return split;
	}

	public void setSplit(boolean split) {
		this.split = split;
	}

	public void setOnchange(String onchange) {
		input.setOnchange(onchange);
	}

	public void setOnclick(String onclick) {
		input.setOnclick(onclick);
	}

	public void setOnfocus(String onfocus) {
		input.setOnfocus(onfocus);
	}

	public void setOnblur(String onblur) {
		input.setOnblur(onblur);
	}

	public String getStyle() {
		return input.getStyle();
	}

    public boolean isRemove() {
        return remove;
    }

    public void setRemove(boolean remove) {
        this.remove = remove;
    }
}
